package TOP101;

import java.util.ArrayList;

/**
 * @Question BM23
 * @Date 2022/11/9 16:01
 * @Solution
 */
public class BM23 {
    ArrayList<Integer> arr = new ArrayList<>();
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        preOrder(root);
        int[] ans=new int[arr.size()];
        int dex=0;
        for(int i=0;i<ans.length;i++) {
            ans[dex++]= arr.get(i);
        }
        return ans;
    }
    public void preOrder(TreeNode root) {
        if(root==null) {
            return;
        }
        arr.add(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }
}
